﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma.YeniYapi.Confirmation
{
    public partial class YeniYuk : XForm
    {

        public int VendorId;
        public YeniYuk(int VendorId)
        {
            InitializeComponent();
            this.VendorId = VendorId;
        }

        DataTable dtSM;
        private void YeniYuk_Load(object sender, EventArgs e)
        {
            bbiKaydet.Enabled = false;

            dtSM = ExecuteDataTable("GetLegalShipMethods", VendorId);
            gleYuklemeSekli.Properties.Assign(RepositoryManager.GetGLESablon(dtSM, "Name", "ShipMethodId"));
            //gleYuklemeSekli.Properties.Assign(RI(Rp.ShipMethod));
            gleYuklemeSekli.EditValueChanged += gleYuklemeSekli_EditValueChanged;

            deYukleme.EditValue = /*deFatura.EditValue = deIrsaliye.EditValue =*/ DateTime.Now;
            deYukleme.EditValueChanged += deYukleme_EditValueChanged;
            //gleYuklemeSekli.EditValue = 1;

            bbiKaydet.ItemClick += bbiKaydet_ItemClick;

            buttonEdit1.Properties.ReadOnly = true;
            buttonEdit1.ButtonClick += buttonEdit1_ButtonClick;
            //buttonEdit1.EditValueChanged += buttonEdit1_EditValueChanged;
            //buttonEdit1.PerformClick(buttonEdit1.Properties.Buttons[0]);

            btnOnceki.ItemClick += btnOnceki_ItemClick;
        }

        void btnOnceki_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (AppSession.OncekiYukleme.AlisFaturasiId > 0)
                buttonEdit1.Text = AppSession.OncekiYukleme.AlisFaturasiId.ToStr();
            else
                buttonEdit1.Text = string.Empty;

            deYukleme.EditValue = AppSession.OncekiYukleme.YuklemeTarihi;
            gleYuklemeSekli.EditValue = AppSession.OncekiYukleme.YuklemeSekliId;
            txtIrsaliyeNo.Text = AppSession.OncekiYukleme.IrsaliyeNo;
            ETAchanged();   //lblETA için
        }

        void deYukleme_EditValueChanged(object sender, EventArgs e)
        {
            ETAchanged();
        }

        void buttonEdit1_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            using (FaturaSec yf = new FaturaSec(VendorId))
                if (yf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    lblFatura.Text = string.Empty;
                    parabirimi = 0;

                    DataTable dt = ExecuteDataTable("GetFaturaInfo", yf.AlisFaturasiId);
                    if (dt.Rows.Count > 0)
                    {
                        int GonderenId = dt.Rows[0]["GonderenId"].ToInt();
                        string FaturaNo = dt.Rows[0]["InvoiceNo"].ToStr();
                        if (AppSession.Lookups.Cari.FindByCariId(GonderenId) != null)
                            lblFatura.Text = FaturaNo + " - " + AppSession.Lookups.Cari.FindByCariId(GonderenId).Name;
                        parabirimi = dt.Rows[0]["CurrencyId"].ToInt();

                    }
                    buttonEdit1.EditValue = yf.AlisFaturasiId;
                    bbiKaydet.Enabled = gleYuklemeSekli.EditValue.ToInt() > 0 && buttonEdit1.EditValue.ToInt() > 0;
                }
        }

        public int parabirimi = 0;
        void bbiKaydet_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (DateToAdd() == 0)
            {
                XtraMessageBox.Show("Üretici ile yükleme şekli tutarsız !", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                AppSession.OncekiYukleme = new AppSession.clsOncekiYukleme();

                if (buttonEdit1.Text != string.Empty)
                    AppSession.OncekiYukleme.AlisFaturasiId = buttonEdit1.Text.ToInt();

                if (deYukleme.EditValue != null)
                    AppSession.OncekiYukleme.YuklemeTarihi = deYukleme.EditValue.ToDate();

                if (gleYuklemeSekli.EditValue != null)
                    AppSession.OncekiYukleme.YuklemeSekliId = gleYuklemeSekli.EditValue.ToInt();

                if (txtIrsaliyeNo.Text != string.Empty)
                    AppSession.OncekiYukleme.IrsaliyeNo = txtIrsaliyeNo.Text;

                if (lblETA.Text != string.Empty)
                    AppSession.OncekiYukleme.ETA = lblETA.Text.ToDate();

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private int DateToAdd()
        {
            int ShipMethod = gleYuklemeSekli.EditValue.ToInt(-1);
            int OriginId = AppSession.Lookups.Vendor.FindByVendorId(VendorId).OriginId.ToInt(-1);
            int add = intParse(AppSession.Lookups.ETA.Compute("max(ETADays)", "(OriginId=" + OriginId + " OR OriginId is null) AND ShipMethodId=" + ShipMethod.ToString()));
            return add;
        }

        private void gleYuklemeSekli_EditValueChanged(object sender, EventArgs e)
        {
            ETAchanged();
        }

        private void ETAchanged()
        {
            int add = DateToAdd();

            DateTime date = deYukleme.DateTime.AddDays(add);
            lblETA.Text = date.ToString("dd.MM.yyyy");
            lblETA.Tag = date;

            bbiKaydet.Enabled = gleYuklemeSekli.EditValue.ToInt() > 0 && buttonEdit1.EditValue.ToInt() > 0;
        }
    }
}
